package com.hjy.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hjy.mapper.UserMapper;
import com.hjy.pojo.User;
import com.hjy.pojo.UserDetail;
import com.hjy.pojo.UserInfo;
import com.hjy.service.UserService;
import com.hjy.utils.UserDetailHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserHandler implements UserDetailHandler {
    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    /**
     * 获取权限和角色信息
     * @param username
     * @return
     */
    @Override
    public UserDetail loadUserByUsername(String username) {
        // 查询语句
        List<String> roleList = userMapper.getRoleList(username);
        List<String> authList = userMapper.getAuthList(username);
        UserDetail userDetail = new UserDetail();
        userDetail.setRoles(roleList);
        userDetail.setAuths(authList);
        return userDetail;
    }

    /**
     * 验证登录是否有效
     * @param userInfo
     * @return
     */
    @Override
    public Boolean loginCorrect(UserInfo userInfo) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",userInfo.getUsername()).eq("password",userInfo.getPassword());
        int count = userService.count();
        if (count > 0) {
            return true;
        }else {
            return false;
        }
    }
}
